package top.yongyan0624.Utils;

import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ObjectMetadata;
import org.springframework.web.multipart.MultipartFile;
import top.yongyan0624.common.lang.Const;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Date;

/**
 * @author Yongyan
 * @description
 * @date 2021/12/30
 **/
public class OSSUtil {

    // 初始化一个OSSClient
    private static final OSS ossClient;

    static {
        // 创建ClientConfiguration实例，您可以根据实际情况修改默认参数。
        ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
        // 开启CNAME，CNAME用于将自定义域名绑定到目标Bucket。
        conf.setSupportCname(true);
        ossClient = new OSSClientBuilder().build(Const.END_POINT, Const.ACCESS_KEY_ID, Const.ACCESS_KEY_SECRET, conf);
    }

    public static OSS getOssClient() {
        return ossClient;
    }

    public static String getImgUrl(MultipartFile avatar) {
        //OSS存储文件
        File file = transferToFile(avatar);
        String fileKey = Const.IMG_PATH + file.getName();
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentType(OSSUtil.getContentType(fileKey.substring(fileKey.lastIndexOf("."))));
        ossClient.putObject(Const.BUCKET_NAME, fileKey, file, objectMetadata);

        //OSS获取图片URL
        Date expiration = new Date(new Date().getTime() + 3600L * 1000 * 24 * 365 * 10);
        URL url = ossClient.generatePresignedUrl(Const.BUCKET_NAME, fileKey, expiration);

        return url.toString();
    }

    public static File transferToFile(MultipartFile multipartFile) {
        //选择用缓冲区来实现这个转换即使用java 创建的临时文件 使用 MultipartFile.transferto()方法 。
        File file = null;
        try {
            // 获取文件名
            String fileName = multipartFile.getOriginalFilename();
            // 获取文件的后缀名
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            //生成文件
            file = File.createTempFile(String.valueOf(System.currentTimeMillis()), suffixName);
            //转换
            multipartFile.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

    public static String getContentType(String FilenameExtension) {
        if (FilenameExtension.equalsIgnoreCase(".bmp")) {
            return "image/bmp";
        }
        if (FilenameExtension.equalsIgnoreCase(".gif")) {
            return "image/gif";
        }
        if (FilenameExtension.equalsIgnoreCase(".jpeg") ||
                FilenameExtension.equalsIgnoreCase(".jpg") ||
                FilenameExtension.equalsIgnoreCase(".png")) {
            return "image/jpg";
        }
        if (FilenameExtension.equalsIgnoreCase(".html")) {
            return "text/html";
        }
        if (FilenameExtension.equalsIgnoreCase(".txt")) {
            return "text/plain";
        }
        if (FilenameExtension.equalsIgnoreCase(".vsd")) {
            return "application/vnd.visio";
        }
        if (FilenameExtension.equalsIgnoreCase(".pptx") ||
                FilenameExtension.equalsIgnoreCase(".ppt")) {
            return "application/vnd.ms-powerpoint";
        }
        if (FilenameExtension.equalsIgnoreCase(".docx") ||
                FilenameExtension.equalsIgnoreCase(".doc")) {
            return "application/msword";
        }
        if (FilenameExtension.equalsIgnoreCase(".xml")) {
            return "text/xml";
        }
        return "image/jpg";
    }
}
